<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="openDialog(1,0)">添加权限</el-button>
      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="openDialog(2,row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="$refs.addPerForm.reviewData(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delRow(row.id)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>
    </div>
    <addPermForm ref="addPerForm" @updateList="initData" />
  </div>
</template>
<script>
import { delPermissionApi, getPermissionList } from '@/api/permission'
import { transListToTreeData } from '@/utils'
import addPermForm from '@/views/permission/components/addPermForm'
export default {
  name: 'Permission',
  components: {
    addPermForm
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const res = await getPermissionList()
      this.list = transListToTreeData(res, 0) // 将列表数据转化成树形结构
    },
    // 打开弹框
    openDialog(type, pid) {
      // type是权限的类型  1是页面权限，  2是按钮权限
      // pid 是父级id,
      this.$refs.addPerForm.formData.pid = pid
      this.$refs.addPerForm.formData.type = type
      this.$refs.addPerForm.showDialog = true
    },
    // 删除行
    async delRow(id) {
      await this.$confirm('确认删除吗?')
      await delPermissionApi(id)
      this.$message.success('删除成功')
      this.initData()
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
